package com.fighYo.trace.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fighYo.trace.entity.Payment;
import com.fighYo.trace.entity.User;
import com.fighYo.trace.entity.vo.UserVo;
import com.fighYo.trace.mapper.PaymentMapper;
import com.fighYo.trace.service.PaymentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fighYo
 * @since 2022-04-13
 */
@Service
public class PaymentServiceImpl extends ServiceImpl<PaymentMapper, Payment> implements PaymentService {

    private void updateSeller(UserVo userVo) {
        QueryWrapper<Payment> wrapper = new QueryWrapper();
        wrapper.eq("seller_id", userVo.getId());
        List<Payment> payments = baseMapper.selectList(wrapper);
        for (Payment payment : payments) {
            payment.setSellerRealName(userVo.getUserRealName());
            payment.setSellerTel(userVo.getTel());
            baseMapper.updateById(payment);
        }
    }

    private void updateBuyer(UserVo userVo) {
        QueryWrapper<Payment> wrapper = new QueryWrapper();
        wrapper.eq("buyer_id", userVo.getId());
        List<Payment> payments = baseMapper.selectList(wrapper);
        for (Payment payment : payments) {
            payment.setBuyerRealName(userVo.getUserRealName());
            payment.setBuyerTel(userVo.getTel());
            baseMapper.updateById(payment);
        }
    }

    @Override
    public void updateUserInfo(UserVo userVo) {
        updateSeller(userVo);
        updateBuyer(userVo);
    }
}
